﻿using Microsoft.Win32;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Xml.Serialization;

namespace XMLTestDataGenerator
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
            BrandGenerator();
            CategoryGenerator();
            ProductGenerator(1000);
        }
        List<string> mærker;
        public void BrandGenerator()
        {
            StreamReader sr = new StreamReader("mærker.txt");
             mærker = new List<string>();
            while (!sr.EndOfStream)
            {
                mærker.Add(sr.ReadLine());
            }
            sr.Close();

            XmlSerializer ser = new XmlSerializer(typeof(BrandFeed));

            List<BrandFeedBrand> feed = new List<BrandFeedBrand>();
            
            for (int i = 1; i < mærker.Count; i++)
            {
                feed.Add(new BrandFeedBrand { BrandCode = i.ToString(), BrandName = mærker[i].ToString() });
            }

            var data = new BrandFeed { Brands = feed.ToArray() };
            
                using (var stream = new StreamWriter(NæsteFilNavn(Directory.GetCurrentDirectory() + @"\GENERATED_brands.xml"))) ser.Serialize(stream, data);
            
           
        }

        List<string> typer;
        public void CategoryGenerator()
        {
            List<string> farver = new List<string>();
            List<string> str = new List<string>();
            typer = new List<string>();

            #region Liste Filler
            StreamReader sr = new StreamReader("Produkter_Farve.txt");

            while (!sr.EndOfStream)
            {
                farver.Add(sr.ReadLine());
            }

            sr.Close();

            sr = new StreamReader("Produkter_Str.txt");

            while (!sr.EndOfStream)
            {
                str.Add(sr.ReadLine());
            }

            sr.Close();

            sr = new StreamReader("Produkter_Typer.txt");

            while (!sr.EndOfStream)
            {
                typer.Add(sr.ReadLine());
            }

            sr.Close(); 
            #endregion

            XmlSerializer ser = new XmlSerializer(typeof(CategoryFeed));
            List<Category> category = new List<Category>();
            Random r = new Random();

            for (int j = 0; j < 3; j++)
            {
                for (int i = 1; i < farver.Count; i++)
                {
                    category.Add(new Category { BrandCode = r.Next(1, mærker.Count).ToString(), Name = "Farve", Description = farver[r.Next(1, farver.Count)].ToString(), Id = i.ToString(), Level = i.ToString() });
                }

                for (int i = 0; i < str.Count; i++)
                {
                    category.Add(new Category { BrandCode = r.Next(1, mærker.Count).ToString(), Name = "Størrelse", Description = str[r.Next(1, str.Count)].ToString(), Id = i.ToString(), Level = i.ToString() });
                }

                for (int i = 0; i < typer.Count; i++)
                {
                    category.Add(new Category { BrandCode = r.Next(1, mærker.Count).ToString(), Name = "Type", Description = typer[r.Next(1, str.Count)].ToString(), Id = i.ToString(), Level = i.ToString() });
                } 
            }

            var data = new CategoryFeed { Categories = category.ToArray() };

            using (var stream = new StreamWriter(NæsteFilNavn(Directory.GetCurrentDirectory() + @"\GENERATED_categories_Farver.xml"))) ser.Serialize(stream, data);

        }

        public void ProductGenerator(int antalProdukter)
        {
            XmlSerializer ser = new XmlSerializer(typeof(ProductFeed));
            Array stati = Enum.GetValues(typeof(ProductFeedProductProductStatus));

            List<ProductFeedProductDescription> productDes = new List<ProductFeedProductDescription>();
            List<string> availablityLevel = new List<string>();
            availablityLevel.Add("A");
            availablityLevel.Add("NA");
            
            List<ProductFeedProduct> products = new List<ProductFeedProduct>();
            Random r = new Random();
            for (int i = 0; i < antalProdukter; i++)
            {
                productDes = new List<ProductFeedProductDescription>();
                productDes.Add(new ProductFeedProductDescription { Description = "Beskrivelse af produktet", Language = "da-DK", ReplacementNotes = "dette er en replacementNote" });
                productDes.Add(new ProductFeedProductDescription { Description = "", Language = "en-US", ReplacementNotes = "This is the en-US Replacement note" });
                productDes.Add(new ProductFeedProductDescription { Description = "", Language = "en-UK", ReplacementNotes = "This is the en-UK replacement note" });
                products.Add(new ProductFeedProduct { ProductNumber = i.ToString(), AvailabilityLevel = availablityLevel[r.Next(2)], BrandCode = r.Next(1, mærker.Count).ToString(), Category = r.Next(1, typer.Count).ToString(), Descriptions = productDes.ToArray(), LaunchDate = TilfældigDato(), LaunchDateSpecified = true, Price = r.Next(15, 2500), PriceSpecified = true, ProductStatus = (ProductFeedProductProductStatus)stati.GetValue(r.Next(stati.Length)) });
            }
            var ri = new RegionInfo(Thread.CurrentThread.CurrentUICulture.LCID);
            var data = new ProductFeed { CreatedTime = DateTime.Now, Currency = ri.ISOCurrencySymbol, NumberOfProducts = products.Count, SalesOrganizationId = "1001", Products = products.ToArray() };

            using (var stream = new StreamWriter(NæsteFilNavn(Directory.GetCurrentDirectory() + @"\GENERATED_products.xml"))) ser.Serialize(stream, data);
        }
        
        public string NæsteFilNavn(string fileName)
        {
            int i = 1;
            string dir = Path.GetDirectoryName(fileName);
            string file = Path.GetFileNameWithoutExtension(fileName) + "{0}";
            string extension = Path.GetExtension(fileName);

            while (File.Exists(fileName))
                fileName = Path.Combine(dir, string.Format(file, "_" + i++) + extension);

            return fileName;
        }
        Random rand = new Random();
        public DateTime TilfældigDato()
        {
            DateTime start = new DateTime(2005,1,1);
            
            int range = int.Parse((DateTime.Today - start).TotalDays.ToString());
            return start.AddDays(rand.Next(range));
        }
    }
}
